/**
 * Copyright 2010 - YangJiandong(chunquedong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 
 * ChunMap是开源软件。你可以自由复制、传播本项目的下载包文件，但必须保持其完整性。
 * 我们不用对使用中的风险及由此造成的损失承担任何责任。
 * 详细情况请见《ChunMap许可协议》。
 * 
 * 想了解更多有关ChunMap的信息，请访问http://code.google.com/p/chunmap/
 * 
 * 下面此段程序作者YangJianDong(chunquedong)
 * 最后修改时间2010.7.17
 */
package chunmap.view.render;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;

import chunmap.data.feature.Feature;
import chunmap.data.feature.Raster;
import chunmap.data.feature.Shape;
import chunmap.model.elem.Envelope;
import chunmap.model.geom.GeoPoint;
import chunmap.model.geom.Geometry;
import chunmap.model.geom.GeometryCollection;
import chunmap.model.geom.LineString;
import chunmap.model.geom.Polygon;
import chunmap.view.View;

public class GeneralSymbol  extends Symbol{

	private Style2D style;
	private DrawHelper drawHelper = new DrawHelper();
	private Graphics2D g;
	private View view;
	
	public GeneralSymbol(){
		this(new Style2D());
	}
	public GeneralSymbol(Style2D style){
		this.style=style;
		drawHelper.setStyle(style);
	}
	
	//可能同时被用来绘图，会出现串色现象
	@Override
	public synchronized void draw(Graphics g, Feature data, View view) {
		if (!style.isVisible())return;
		
		this.g=(Graphics2D)g;
		this.view=view;
		
		if(data instanceof Shape){
			Shape shp=(Shape)data;
			drawShape(shp);
		}else if(data instanceof Raster){
			Raster shp=(Raster)data;
			drawRaster(shp);
		}
	}

	private void drawRaster(Raster tile) {
		// draw raster
		Image image=tile.getImage();
		Envelope env = tile.getEnvelop().transform(view.world2Screen());
		if ( image!= null) {
			double x=(int)(env.getMinX()-0.5);
			double y=(int)(env.getMinY()-0.5);
			double w=env.getWidth()+0.5;
			double h=env.getHeight()+0.5;
			
			drawHelper.drawImage(g, image, (int)x , (int) y, (int)w ,(int)h);
			// drawHelper.drawLineString(g, env.toPolygon().getShell(),
			// (Style2D) defaultStyle);
		}else{
			g.setColor(Color.black);
			g.drawString("no data. engine by Chun-Map"
					, (int)env.getCenter().getX(), (int)env.getCenter().getY());
		}
	}
	

	private void drawShape(Shape shape) {
		Geometry geo = shape.getGeometry();
		if (geo == null) return;
		
		draw(geo);
	}
	
	// -----------------------------------------------------------画几何体

	private void draw(Geometry geo) {
		selectDraw(geo);
	}

	private void selectDraw(Geometry geo) {
		if (geo instanceof GeoPoint) {
			drawHelper.drawPoint(g, geo,view);
		} else if (geo instanceof LineString) {
			drawHelper.drawLineString(g, geo,view);
		} else if (geo instanceof Polygon) {
			drawHelper.drawPolygon(g, geo,view);
		} else if (geo instanceof GeometryCollection) {
			GeometryCollection geoCollection = (GeometryCollection) geo;
			for (Geometry geo2 : geoCollection) {
				draw(geo2);
			}
		} else {
			throw new UnsupportedOperationException();
		}
	}
}
